/**
 * Class BillingPage.
 * 
 * @author Jean-Luc Scheefer
 */
BillingPage.inherits(Module);
function BillingPage() {
	var that = this;
	
	// Inputs module
	this.inputBeginDate = new InputDateModule({
		'id' : 'inputBeginDate',
		'label' : I18n.get('begin.date'),
		'placeholder' : I18n.get('begin.date'),
		'maxDate' : moment().add('days', 1),
		'value' : moment().subtract('weeks', 1),
		'onChange' : function(){
			var start = that.inputBeginDate.getMillis();
			that.inputEndDate.setMinDate(moment(start));
			if (Util.checkStringEmpty(that.inputEndDate.getValue())){
				that.inputEndDate.setValue(that.inputEndDate.maxDate);
			}
			that.inputEndDate.buildJQ();
		}
	});
	this.inputEndDate = new InputDateModule({
		'id' : 'inputEndDate',
		'label' : I18n.get('end.date'),
		'placeholder' : I18n.get('end.date'),
		'minDate' : moment().add('days', 1),
		'maxDate' : moment().add('days', 1),
		'value' : moment()
	});
	
	//input referrer
	this.inputReferrer = new InputReferrerModule({
		'id' : 'inputReferrer',
		'label' : I18n.get('entity'),
		'value' : UserManager.user.referrer,
		'rootReferrer' : UserManager.user.referrer,
		'mandatory' : true
	});
	
	//results 
	this.createdMandatesCount = $('<b class="billing-value">-</b>');
	this.remitOrdersCount = $('<b class="billing-value">-</b>');
}

BillingPage.neededRoles = ["ROLE_USER", "VIEW_REPORTING", "VIEW_BILLING"];

BillingPage.prototype.fillJQ = function(jQ) {
	var that = this;
	
	// Title
	jQ.append($('<h4>' + I18n.get('billing.pageTitle') + '</h4>'));
	
	// Form
	var form = $('<div class="form well row-fluid"></div>');
	jQ.append(form);
	var columnLeft = $('<div class="span6"></div>');
	columnLeft.append(this.inputBeginDate.buildJQ());
	columnLeft.append(this.inputReferrer.buildJQ());
	var columnRight = $('<div class="span6"></div>');
	columnRight.append(this.inputEndDate.buildJQ());
	form.append(columnLeft).append(columnRight);
	// Buttons
	var buttonRow = $('<div class="btn-row"></div>');
	form.append(buttonRow);
	var buttonSearch = $('<div class="btn search">' + I18n.get('search') + '</div>');
	buttonSearch.click(function() {
		that.loadBilling();
	});
	buttonRow.append(buttonSearch);
	
	//results
	form = $('<div style="text-align:left;" class="form well row-fluid"></div>');
	jQ.append(form);
	form.append($('<div class="billing-values">' + I18n.get('billing.createdMandatesCount') + ' : </div>').append(this.createdMandatesCount));
	form.append($('<div class="billing-values">' + I18n.get('billing.remitOrdersCount') + ' : </div>').append(this.remitOrdersCount));
	
	setTimeout(function(){that.loadBilling();}, 500);
};

BillingPage.prototype.loadBilling = function(){
	var that = this;
	AjaxManager.getJSON('getBillingFacts', {
		'beginDate' : this.inputBeginDate.getMillis(),
		'endDate' : this.inputEndDate.getMillis(),
		'referrerId' : this.inputReferrer.value.id
	}, function(result) {
		if (result.success) {
			if (Util.checkObject(result.billingFacts)){
				that.createdMandatesCount.html(result.billingFacts.createdMandatesCount);
				that.remitOrdersCount.html(result.billingFacts.remitOrdersCount);
			} else {
				PopupModule.getInstance().show({
					'title' : I18n.get('billing.noResultT'),
					'content' : I18n.get('billing.noResult'),
					'button0' : { 'text' : 'OK' }
				});
			}
		}
	});
};
